package com.ihome.android.market2.asynctask;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ihome.android.EAApp;
import com.ihome.android.academy.AppMgrActivity;
import com.ihome.android.academy.DetailActivity;
import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.market2.view.DialogFactory;
import com.ihome.android.market2.view.DownloadDialog;
import com.ihome.android.market2.view.MyProgressDialog;
import com.ihome.android.market2.view.DialogFactory.onBtnClickListener;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;
import com.ihome.android.util.log.UserBehaviorLog;
import com.ihome.android.util.log.UserBehaviorLog.NotifyType;
import com.ihome.android.util.log.UserBehaviorLog.UserBehavior;

/**
 * 获取手机验证码
 * 
 * @author Emerson
 * 
 */
public class GetAppUrlTask extends AsyncTask<Bundle, Integer, Bundle> {
	private final String		TAG		= GetAppUrlTask.class.getSimpleName();
	private Context				context;
	private MyProgressDialog	progressDialog;
	private String				appUrl	= "";
	private String				appName	= "";
	private String				pkgName	= "";
	private String				iconUrl	= "";
	private String				appMD5		= "";

	public GetAppUrlTask(Context context) {
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog = MyProgressDialog.getDialog(context, context.getString(R.string.sending_a_request));
		progressDialog.show();
		progressDialog.setCancelable(true);
		progressDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (GetAppUrlTask.this.getStatus().compareTo(AsyncTask.Status.FINISHED) != 0)
					GetAppUrlTask.this.cancel(true);
			}
		});
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = params[0];
		int appID = b.getInt(HttpKeys.KEY_APPID);
		appName = b.getString(HttpKeys.KEY_APPNAME, "" + appID);
		pkgName = b.getString(HttpKeys.KEY_PACKAGENAME, "");
		iconUrl = b.getString(HttpKeys.KEY_ICONURL, "");
		LogUtil.v(TAG, "appID = " + appID);
		if (appID <= 0) {
			return b;
		}

		try {

			String dlApp = new HttpActions(context).downApp(0, "" + appID, 0);
			LogUtil.v(TAG, "getAppUrl result-->" + dlApp);
			if (!TextUtils.isEmpty(dlApp)) {
				appUrl = JsonAnalytic.getInstance().anslyseAppDLUrlResult(dlApp);
				appMD5 = JsonAnalytic.getInstance().anslyseAppDLMD5Result(dlApp);
			}
			else {
				cancel(true);
			}

		} catch (ITVException e) {
			String[] msg1 = { "", "", "" , "", "trade.downApp", "" };
			UserBehaviorLog.getInstance().writeLog(UserBehavior.STORERUN_EXCEPTION, NotifyType.TYPE_FAILURE, "", msg1);
			e.printStackTrace();
			LogUtil.e(TAG, e.toString());
			b.putInt(HttpKeys.KEY_CODE, e.getStatusCode());
			b.putString(HttpKeys.KEY_DESCRIPTION, e.getMessage());
		}
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);
		if (progressDialog != null) {
			progressDialog.dismiss();
		}
		if (result == null) {
			return;
		}

		/** 请求出错处理 */
		if (result.containsKey(HttpKeys.KEY_CODE)) {
			if(ITVException.CODE_LOGINOUT_THREE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGINOUT_ONE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_SESSIONKEY == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGIN_ONE == result.getInt(HttpKeys.KEY_CODE)){
				new DialogFactory(context).showDialog("",result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")",context.getString(R.string.dialog_ok),new onBtnClickListener() {
					
					@Override
					public void btnRightClickListener(View v) {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void btnNeutralClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void btnLeftClickListener(View v) {
						// TODO Auto-generated method stub
						EAApp.getApp().exitApp(true);
					}
					
					@Override
					public void btnCloseClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
				});
				return;
			}
			new DialogFactory(context).showDialog(result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")");
			return;
		}

		if (context instanceof DetailActivity) {
			((DetailActivity) context).startDLAPK(appUrl, appName, pkgName,appMD5);
			return;
		}

		if (context instanceof AppMgrActivity) {
			((AppMgrActivity) context).startDLAPK(appUrl, appName, pkgName, iconUrl,appMD5);
			return;
		}

		if (!TextUtils.isEmpty(appUrl)) {
			String appName = result.getString(HttpKeys.KEY_APPNAME);
			LogUtil.v(TAG, "To DownloadDialog ....");
			new DownloadDialog(context, appName, appUrl,pkgName,appMD5).show();
		}
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");
		if (progressDialog != null)
			progressDialog.dismiss();
	}

}
